/**  
 * 
 * @Title:  IndexUtil.java   
 * @Package com.bicon.datamanager.util   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: 123774135@qq.com     
 * @date:   2018年7月17日 下午2:15:37   
 * @version V1.0 
 * @Copyright: 2018 www.tydic.com Inc. All rights reserved. 
 * 
 */
package com.bicon.base.data.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

import com.bicon.base.data.common.StringTools;
import com.bicon.base.data.common.tools.DateUtil;
import com.google.common.collect.Lists;

/**
 * @ClassName: IndexUtil
 * @Description:各个指标的计算
 * @author: 123774135@qq.com
 * @date: 2018年7月17日 下午2:15:37
 * 
 * @Copyright: 2018
 * 
 */
public class IndexUtil {

	/**
	 * 年龄分界线，以后这个要配置在参数里面进行配置
	 */
	private static final double AGE_DIVIDING_LINE = 25;

	private static final String PERCENT_FORMATE = "0.0%";

	private static final String SEX_STR = "男";

	private static final int MAN_CODE = 1;

	/**
	 * BMI（身体质量指数) 
	 * @Title: computeBmiIndex 
	 * @Description:BMI（身体质量指数) 
	 * @param: height 
	 * @param:weight @
	 * @return: String 
	 * @throws
	 */
	public static String bmiIndex(double height, double weight) {
		// 体重/(身高/100)*(身高/100)
		DecimalFormat df = new DecimalFormat("0.0");
		// Product p = new Product();
		double res = weight / Math.pow((height / 100), 2);
		
		return df.format(new BigDecimal(res));
	}

	 
	/**
	 * 
	 * @Title: ageEffectOfIndex 
	 * @Description: 年龄影响 年龄影响//=-ROUND(ABS(年龄-25)/5,0) 
	 * @param:age  
	 * @return: String 
	 * @throws
	 */
	public static String ageEffectOfIndex(double age) {
		double res = Math.abs(age - AGE_DIVIDING_LINE);
		long round = Math.round(res / 5);
		if (age - AGE_DIVIDING_LINE > 0) {
			return String.valueOf(0 - round);
		} else {
			return String.valueOf(round);
		}

	}

	/**
	 * (1.2*BMI指数+0.23*年龄-5.4-10.8*IF(B3="男",1,0))/100 
	 * @Title:bodyfatrateIndex (1.2*BMI指数+0.23*年龄-5.4-10.8*IF(B3="男",1,0))/100
	 * @Description: 体脂率 
	 * @param:height：身高
	 * @param:weight：体重
	 * @param:age：年龄
	 * @param:sex：性别
     * @return: String 
     * @throws
	 */
	public static String bodyfatrateIndex(double height, double weight, double age, String sex) {
		int sex_code = SEX_STR.equals(sex) ? MAN_CODE : (MAN_CODE - 1);
		DecimalFormat df = new DecimalFormat(PERCENT_FORMATE);
		double res = (1.2 * Double.parseDouble(bmiIndex(height, weight)) + 0.23 * age - 5.4 - 10.8 * sex_code) / 100;
		return df.format(new BigDecimal(res));
	}
	
	public static String bodyfatrateIndexformate(double height, double weight, double age, String sex) {
		int sex_code = SEX_STR.equals(sex) ? MAN_CODE : (MAN_CODE - 1);
		DecimalFormat df = new DecimalFormat("#.00");
		double res = (1.2 * Double.parseDouble(bmiIndex(height, weight)) + 0.23 * age - 5.4 - 10.8 * sex_code) / 100;
		return df.format(new BigDecimal(res));
	}
	
	

	// IF(((1-ABS(BMI-21.2)/21.2)*100+年龄影响)<60,60,((1-ABS(BMI-21.2)/21.2)*100+年龄影响))
	/**
	 * 
	 * @Title: healthscore 基础健康评分 
	 * @Description:IF(((1-ABS(BMI-21.2)/21.2)*100+年龄影响)<60,60,((1-ABS(BMI-21.2)/21.2)*100+年龄影响))健康评分打分 
	 * @param:height 
	 * @param:weight 
	 * @param:age 
	 * @return:String 
	 * @throws
	 */
	public static String healthscore(double height, double weight, double age) {
		// (1-ABS(BMI-21.2)/21.2)*100+年龄影响
		double bmiIndex = Double.parseDouble(bmiIndex(height, weight));
		double ageEffectOfIndex = Double.parseDouble(ageEffectOfIndex(age));
		double res = (1 - Math.abs(bmiIndex - 21.2) / 21.2) * 100 + ageEffectOfIndex;
		DecimalFormat df = new DecimalFormat("0.0");
		return df.format(new BigDecimal(res < 60 ? 60:res));
	}

	// =IF(B3="男",身高*0.415,B4*0.413)
	/**
	 * 
	 * @Title: pacelength 
	 * @Description: 步伐长度计算 
	 * @param: sex 
	 * @param: height 
	 * @return: String 
	 * @throws
	 */
	public static String pacelength(String sex, double height) {
		double res = SEX_STR.equals(sex) ? height * 0.415 : height * 0.413;
		DecimalFormat df = new DecimalFormat("#.00");
		return df.format(new BigDecimal(res));
	}

	// =SUM(M16:M27)食物获取卡路里
	// public static String foodConverttoCalorie() {
	//
	// }

	// 
	/**
	 * 
	 * @Title: basicmetabolicrate 
	 * @Description: BMR（基础代谢率） BMR（基础代谢率）=IF(B3="女",(65.5+9.6*体重+1.8*身高-4.7*年龄),(66+13.7*B5+5*B4-6.8*E4))
	 * @param: height 
	 * @param: weight 注意是公斤的单位 
	 * @param: age 
	 * @param: sex 
	 * @return:String 
	 * @throws
	 */
	public static String basicmetabolicrate(double height, double weight, double age, String sex) {
		double res = 0;
		if (SEX_STR.equals(sex)) {
			res = 66 + 13.7 * weight + 5 * height - 6.8 * age;
		} else {
			res = 655 + 9.6 * weight + 1.8 * height - 4.7 * age;
		}
		DecimalFormat df = new DecimalFormat("#.00");
		return df.format(new BigDecimal(res));
	}

	// =21.2*(身高/100)^2
	/**
	 * 
	 * @Title: idealweight 理想体重 
	 * @Description: 21.2*(身高/100)^2 理想体重 
	 * @param: height 
	 * @param: 
	 * @return:String 
	 * @throws
	 */
	public static String idealweight(double height) {
		double res = 21.2 * Math.pow(height / 100, 2);
		DecimalFormat df = new DecimalFormat("#.0");
		return df.format(new BigDecimal(res));
	}

	// =IF(B3="女",(65.5+9.6*E13+1.8*B4-4.7*E4),(66+13.7*E13+5*B4-6.8*E4))
	/**
	 * IF(B3="女",(65.5+9.6*理想体重+1.8*升高-4.7*年龄),(66+13.7*E13+5*B4-6.8*E4)) 
	 * @Title:ldealBmr 理想BMR 
	 * @Description: 理想BMR指数 
	 * @param: 
	 * @return: String 
	 * @throws
	 */
	public static String idealBmr(double height, double weight, double age, String sex) {
		double res = 0;
		if (SEX_STR.equals(sex)) {
			res = 66 + 13.7 * Double.parseDouble(idealweight(height)) + 5 * height - 6.8 * age;
		} else {
			res = 65.5 + 9.6 * Double.parseDouble(idealweight(height)) + 1.8 * height - 4.7 * age;
		}
		DecimalFormat df = new DecimalFormat("#");
		return df.format(new BigDecimal(res));
	}

	// 
	/**
	 * 
	 * @Title: bloodpressureImpact 
	 * @Description: 血糖影响空腹 =IF(IF(J38>6.1,ROUND(J38-6.1,0)*(-2),IF(J38>=3.9,0,ROUND(3.9-J38,0)*(-2)))<-10,-10,IF(J38>6.1,ROUND(J38-6.1,0)*(-2),IF(J38>=3.9,0,ROUND(3.9-J38,0)*(-2.5))))血糖影响空腹,J38:血糖值
	 *  @param: @param bloodvalue 
	 *  @param: 
	 *  @return @return: String 
	 *  @throws
	 */
	public static String bloodpressureImpact(double bloodvalue) {
		double res = 0;
		if (bloodvalue > 6.1) {
			res = Math.round(bloodvalue - 6.1) * (-2);
		} else if (bloodvalue >= 3.9 && bloodvalue < 6.1) {
			// res = Math.round(3.9- bloodvalue)*(-2);
			res = 0;
		} else {
			res = Math.round(3.9 - bloodvalue) * (-2.5);
		}
		DecimalFormat df = new DecimalFormat("0.00");
		return df.format(new BigDecimal(res));
	}

	// =IF(IF(J38>7.8,ROUND(J38-7.8,0)*(-3.4),IF(J38>=4.4,0,ROUND(4.4-J38,0)*(-2)))<-10,-10,IF(J38>7.8,ROUND(J38-7.8,0)*(-3.4),IF(J38>=4.4,0,ROUND(4.4-J38,0)*(-2.5))))血糖影响空腹,J38:血糖值
	/**
	 * 
	 * @Title: bloodpressureImpact 
	 * @Description: 血糖影响（餐后） 
	 * @param: @param
	 * bloodvalue 
	 * @param: @return 
	 * @return: String @throws
	 */
	public static String bloodpressureImpactaftrefood(double bloodvalue) {
		double res = 0;
		if (bloodvalue > 7.8) {
			res = Math.round(bloodvalue - 7.8) * (-3.4);
		} else if (bloodvalue >= 4.4 && bloodvalue < 7.8) {
			// res = Math.round(3.9- bloodvalue)*(-2);
			res = 0;
		} else {
			res = Math.round(4.4 - bloodvalue) * (-2.5);
		}
		DecimalFormat df = new DecimalFormat("0.00");
		return df.format(new BigDecimal(res));
	}

	// 血压影响(高压)=-IF(IF(J39>139,ROUND((J39-139)/10,0),IF(J39>=90,0,ROUND((90-J39)/10,0)))>5,5,IF(J39>139,ROUND((J39-139)/10,0),IF(J39>=90,0,ROUND((90-J39)/10,0))))
	/**
	 * 高压 
	 * @Title: InfluenceOfbloodpressureforhighpressure 
	 * @Description: 血压影响(高压) 
	 * @param:  highpressurevalue 
	 * @param: 
	 * @return : String 
	 * @throws
	 */
	public static String InfluenceOfbloodpressureforhighpressure(double highpressurevalue) {
		double res = 0;
		if (highpressurevalue > 139) {
			res = Math.round(highpressurevalue - 139) / 10.0;
		} else if (highpressurevalue >= 90 && highpressurevalue < 139) {
			res = 0;
		} else {
			res = Math.round(90 - highpressurevalue) / 10.0;
		}
		DecimalFormat df = new DecimalFormat("0.00");
		return df.format(new BigDecimal(res));
	}

	// 
	/**
	 * 低压 
	 * @Title: InfluenceOfbloodpressureforhighpressure 
	 * @Description:血压影响(低压) 血压影响(低压)=-=IF(-IF(J40>89,ROUND((J40-89)/6,0),IF(J40>=60,0,ROUND((60-J40)/6,0)))<-5,-5,-IF(J40>89,ROUND((J40-89)/6,0),IF(J40>=60,0,ROUND((60-J40)/6,0))))
	 * @param: highpressurevalue @
	 * @return:String @
	 * throws
	 */
	public static String influenceOfbloodpressureforlowpressure(double lowpressurevalue) {
		double res = 0;
		if (lowpressurevalue > 89) {
			res = Math.round(lowpressurevalue - 89) / 6.0;
		} else if (lowpressurevalue >= 60 && lowpressurevalue < 89) {
			res = 0;
		} else {
			res = Math.round(60 - lowpressurevalue) / 10.0;
		}
		DecimalFormat df = new DecimalFormat("0.00");
		return df.format(new BigDecimal(res));
	}

	// 
	/**
	 * 
	 * @Title: heartrate 
	 * @Description: 心率影响 心率影响=IF(-IF(J41>100,ROUND((J41-100)/10,0),IF(J41>=60,0,ROUND((60-J41)/10,0)))<-10,-10,-IF(J41>100,ROUND((J41-100)/10,0),IF(J41>=60,0,ROUND((60-J41)/10,0))))
	 * @param:heartrate 
	 * @return: String 
	 * @throws
	 */
	public static String heartrate(double heartrate) {
		double res = 0;
		if (heartrate > 100) {
			res = Math.round(heartrate - 100) / 10.0;
		} else if (heartrate >= 60 && heartrate < 100) {
			res = 0;
		} else {
			res = Math.round(60 - heartrate) / 10.0;
		}
		DecimalFormat df = new DecimalFormat("0");
		return df.format(new BigDecimal(0 - res));
	}

	// 体温评分=-IF(IF(J42>37.2,ROUND((J42-37.2)*2,0),0)>5,5,IF(J42>37.2,ROUND((J42-37.2)*2,0),0))
	/**
	 * 
	 * @Title: temperaturescore 
	 * @Description: 体温评分
	 *  @param: temperature 
	 *  @param: 
	 *  @return: String 
	 *  @throws
	 */
	public static String temperaturescore(double temperature) {
		double res = 0;
		if (temperature > 37.2) {
			res = Math.round(temperature - 37.2) / 2.0;
		} else {
			res = 5;
		}
		DecimalFormat df = new DecimalFormat("0.0");
		return df.format(new BigDecimal(0 - res));

	}

	// 睡眠时长=-((J45-J44)+(J47-J46))*1440
	/**
	 * 
	 * @Description: 睡眠时长=-((J45-J44)+(J47-J46))*1440 
	 * @param:sleep 
	 * @return: String 
	 * @throws
	 */
	public static String sleepduration(List<PairVo<String, String>> pairvos, String formate) {
		double sum = 0;
		for(PairVo<String, String> pairVo2 : pairvos) {
			String start = pairVo2.getStart();
			String end = pairVo2.getEnd();
			// 计算两个时间只差,
			double result = DateUtil.getTimeIntevalMinutes(end, start, formate);
			sum = sum + result;
		}
		
		// 计算公式
		DecimalFormat df = new DecimalFormat("0.0");
		return df.format(new BigDecimal(sum));

	}
	
	//睡眠时长评分==IF(J51-J48>60,0,IF(J51-J48>0,2.5,5))
	/**
	 * 
	 * @Title: sleepdurationScore   
	 * @Description: 睡眠时长评分==IF(J51-J48>60,0,IF(J51-J48>0,2.5,5)) J51 需求睡眠时长,J48: 实际睡眠时长
	 * @param sleepduration  实际睡眠时长
	 * @param demandsleeptime 需求睡眠时长
	 * @return      
	 * String      
	 * @throws
	 */
	public static String sleepdurationScore(double sleepduration,double demandsleeptime) {
		double sleep_difference  = sleepduration - demandsleeptime  ;
		double sum = 0;
		if(sleep_difference > 60) {
			sum = 0;
		}else if(sleep_difference >0 && sleep_difference <= 60){
			sum = 2.5;
		}else {
			sum = 5;
		}
		DecimalFormat df = new DecimalFormat("0.0");
		return df.format(new BigDecimal(sum));
	}

	// 入睡时间评分
	/**
	 * 
	 * @Title: sleepTimescore 
	 * @Description:入睡时间评分=IF((J44-J43)*1440>60,0,IF((J44-J43)*1440>0,2.5,5)),其中j43是基准时间  
	 * @param: pairvo 
	 * @param:formate 
	 * @return: String 
	 * @throws
	 */
	public static String sleepTimescore(PairVo<String, String> pairvo, String formate) {
		String start = pairvo.getStart();// 基准时间
		String end = pairvo.getEnd();
		double end_res = 0;
		// 计算两个时间只差,
		double minutes = DateUtil.getTimeIntevalMinutes(end, start, formate);
		if (minutes > 60) {
			end_res = 0;
		} else  if(minutes > 0 && minutes<= 60){
			end_res = 2.5;
		}else {
			end_res = 5;
		}
		// 计算公式
		DecimalFormat df = new DecimalFormat("0.0");
		return df.format(new BigDecimal(end_res));
	}

	/**
	 * 
	 * @Title: sleepScore 
	 * @Description: 睡眠评分   IF(OR(M44=TRUE,M45=TRUE)=TRUE,0,J49+J52)             =J49+J52其中J49代表入睡时间评分，J50辅助参数，J51:需求睡眠时间J52睡眠时长评分
	 *  @param: sleepTimescore 入睡时间评分
	 *  @param: date辅助参数 
	 *  @param: age 年龄计算需求睡眠
	 *  @param: sleepdurationScore睡眠时长评分 
	 *  @return:String 
	 *  @throws
	 */
	public static String sleepScore(double sleepTimescore, String date, double age, double sleepdurationScore) {
//Double.parseDouble(demandsleeptime(age))
		// 计算公式,求和
		if(sleepTimescore == 0 || sleepdurationScore == 0) {
			return "0.0";
		}
		double result = sleepTimescore +   sleepdurationScore;
		DecimalFormat df = new DecimalFormat("0.0");
		return df.format(new BigDecimal(result));
	}

	
	
	// 需求睡眠时间=(21-6*E4^0.3+E4/10)*60,其中E4代表年龄
	/**
	 * 
	 * @Title: demandsleeptime 
	 * @Description:需求睡眠时间=(21-6*E4^0.3+E4/10)*60,其中E4代表年龄
	 * @param: age
	 * @return: String 
	 * @throws
	 */
	public static String demandsleeptime(double age) {
		double result = (21 - 6 * Math.pow(age, 0.3) + age / 10) * 60;
		// 计算公式
		DecimalFormat df = new DecimalFormat("0.0");
		return df.format(new BigDecimal(result));

	}

	// 用药记录打分=IF(J54="√",IF(ABS(L54)>=60,1,2),-2)，其中L54表示为用药时间间隔值
	/**
	 * 
	 * @Title: drugusescore 
	 * @Description:用药记录打分=IF(J54="√",IF(ABS(L54)>=60,1,2),-2)，其中L54表示为用药时间间隔值 
	 * @param: score 
	 * @param: 
	 * @return String 
	 * @throws
	 */
	public static String drugusescore(double score) {
		double res = Math.abs(score);
		int i = 0;
		if (res >= 60) {
			i = 1;
		} else if (res >= 0 && res < 60) {
			i = 2;
		} else {
			i = -2;
		}
		// 计算公式
		DecimalFormat df = new DecimalFormat("0");
		return df.format(new BigDecimal(i));
	}

	// 用药记录打分求和==IF(SUM(M54:M56)>10,10,IF(SUM(M54:M56)<-10,0,SUM(M54:M56)))
	/**
	 * 
	 * @Title: drugusescoreforsum 
	 * @Description:用药记录打分求和==IF(SUM(M54:M56)>10,10,IF(SUM(M54:M56)<-10,0,SUM(M54:M56)))M54-M56表示各个阶段的评分
	 * @param:  score 
	 * @param:  
	 * @return: String @throws
	 */
	public static String drugusescoreforsum(double[] scores) {
		double result = 0;
		double score = 0;
		for (int i = 0; i < scores.length; i++) {
			result = scores[i] + result;
		}
		if (result > 10) {
			score = 10;
		} else if (result < -10) {
			score = 0;
		} else {
			score = result;
		}
		// 计算公式
		DecimalFormat df = new DecimalFormat("0");
		return df.format(new BigDecimal(score));
	}

	/**
	 * =IF(J54="√",(K54-I54)*1440,0) 
	 * @Title: drugusescore 
	 * @Description:用药时间间隔计算其中k54和I54表示用药时间 
	 * @param: @return: 
	 * void @throws
	 */
	public static String druguseduration(PairVo<String, String> pairvo, String formate) {
		if (StringTools.isBlank(pairvo.getEnd())) {
			return "-2";
		}
		String start = pairvo.getStart();
		String end = pairvo.getEnd();
		// 计算两个时间只差,
		double result = DateUtil.getTimeIntevalMinutes(end, start, formate);
		DecimalFormat df = new DecimalFormat("0.0");
		return df.format(new BigDecimal(result));
		//return sleepduration(pairvo, formate);
	}

	// 日评分=IF(IF(E7+E17+E18+E19+E22+E23+E24+E25+E26>100,100,E7+E17+E18+E19+E22+E23+E24+E25+E26)<60,60,IF(E7+E17+E18+E19+E22+E23+E24+E25+E26>100,100,E7+E17+E18+E19+E22+E23+E24+E25+E26))
	/**
	 * =IF(IF(E27>=B27,E30+B17+B19+B22+B23+B24+B25+B26,E27)<60,60,IF(E27>=B27,E30+B17+B19+B22+B23+B24+B25+B26,E27))
	 * @Title: dayscore 
	 * @Description: 日最终评分=IF(IF(E27>=B27,E30+B17+B19+B22+B23+B24+B25+B26,E27)<60,60,IF(E27>=B27,E30+B17+B19+B22+B23+B24+B25+B26,E27))),E27
	 * @param: height 身高
	 * @param: weight 体重
	 * @param: age 年龄
	 * @param: bloodvalue 血压值 
	 * @param: highpressurevalue 高压 
	 * @param: lowpressurevalue 低压
	 * @param: heartrate  心率
	 * @param: temperature  温度
	 * @param:  sleepScore 睡眠评分
	 * @param: drugusescoreforsum 用药评分 
	 * @return @return:
	 * String @throws
	 */
	public static String dayscore(double height, double weight, double age, double bloodvalue,double afterbloodvalue, double highpressurevalue,
			double lowpressurevalue, double heartrate, double temperature, double sleepScore,
			double drugusescoreforsum) {
		// 计算公式
		double sum = Double.parseDouble(healthscore(height, weight, age))
				+ Double.parseDouble(bloodpressureImpact(bloodvalue))
				+ Double.parseDouble(bloodpressureImpactaftrefood(afterbloodvalue)) + highpressurevalue
				+ lowpressurevalue + heartrate + temperature + sleepScore + drugusescoreforsum;
		DecimalFormat df = new DecimalFormat("0");
		return df.format(new BigDecimal(sum));
	}
	
//	//日初步评分IF(IF(E7+E17+E18+E19+E22+E23+E24+E25+E26>100,100,E7+E17+E18+E19+E22+E23+E24+E25+E26)<60,60,IF(E7+E17+E18+E19+E22+E23+E24+E25+E26>100,100,E7+E17+E18+E19+E22+E23+E24+E25+E26))
//	//E7基础健康评分,E17=饮食评分,E18=运动评分,e19=血糖影响(除餐后),e22=血压影响(高压/低压),e23=心率影响,e24=体温评分,e25=睡眠评分,e26=用药评分
	public static String dailypreliminaryscore(double dietpoint,double sportpoint,double booldpoint,double booldpresspoint,double heartratiopoint,double temppoint,double sleeppoint,double drug) {
		double sum =  dietpoint + sportpoint + booldpoint + booldpresspoint + heartratiopoint + temppoint + sleeppoint + drug;
	    String result = "";
		if(sum > 100) {
			result = String.valueOf(100);
		}else if(sum< 60) {
			result = String.valueOf(60);
		}else {
			result = String.valueOf(sum);
		}
		return result;
	}  
	
	//日最终评分=IF(IF(E27>=B27,E30+B17+B19+B22+B23+B24+B25+B26,E27)<60,60,IF(E27>=B27,E30+B17+B19+B22+B23+B24+B25+B26,E27))
	//E27:日初步评分,B27:修正阙值,E30:赋值辅助计算常量 100，B17：饮食修正值,B19:血糖修正值 ,B22:血压修正值,B23:心率影响修正值,B24:体温修正值
	//B25:睡眠修正值;B26:用药修正值
	public static String dailyendscore(double e27,double b27,double e30,double b17,double b19,double b22,double b23,double b24,double b25,double b26) {
		double result = 0;
		if(e27 > b27) {
			result = e30+b17+b19+b22+b23+b24+b25+b26;
		}else {
			result = b27;
		}
		if(result < 60) {
			result = 60;
		}
		DecimalFormat df = new DecimalFormat("0.0");
		return df.format(new BigDecimal(result));
	}
	
	/**
	 * 
	 * @Title: modifyFunction   
	 * @Description: 修正公式IF(E27>=B27,IF(E22<-8,-5,IF(E22<-6,-4,IF(E22<-4,-2,IF(E22<0,-1,0)))),0)   
	 * @return      
	 * String      
	 * @throws
	 */
	public static String modifyFunction(double e27,double b27,double e22) {
		String result = "";
		if(e27 > b27) {
			if(e22 < -8 ) {
				result = "-5"; 
			}else if(e22 >=-8 && e22 < -6 ) {
				result = "-4"; 
			}else if(e22 >=-6 && e22 < -4 ) {
				result = "-2"; 
			}else if(e22 >=-4 && e22 < 0 ) {
				result = "-1"; 
			}
		}else {
			result = "0";
		}
		return StringTools.isBlank(result)? "0":result;
	}
	
	//基础评分（70%）=E7*0.7 E7:基础健康评分
	public static String basicscore(double height, double weight, double age ) {
		double result = Double.parseDouble(healthscore(height,weight,age)) * 0.7;
		DecimalFormat df = new DecimalFormat("0.0");
		return df.format(new BigDecimal(result));
	}
	
	//行为习惯(30%)(E17+E18+E25)*((100-E5-E7*0.7)/30) E17:饮食评分,E18:运动评分 E25:睡眠评分,E5:年龄影响,E7基础健康评分
	public static String behavioralscore(double dietpoint,double sportpoint,double sleeppoint,double ageimport,double basichealth) {
		double result = Math.abs((dietpoint+sportpoint+sleeppoint)*((100-ageimport-basichealth*0.7) / 30));
		DecimalFormat df = new DecimalFormat("0");
		return df.format(new BigDecimal(result));
	}
	
	//步数系数计算M31*M33/100/1000,其中M31是步数，M33是步距
	/**
	 * 
	 * @Title: stepnumbercoefficient   
	 * @Description: 步数系数计算M31*M33/100/1000,其中M31是步数，M33是步距
	 * @param stepnumber 步数
	 * @param distance 步距     
	 * void      
	 * @throws
	 */
	public static String stepnumbercoefficient(int stepnumber,double distance) {
		double result = stepnumber*distance/100/1000;
		DecimalFormat df = new DecimalFormat("0");
		return df.format(new BigDecimal(result));
	}
	
	//评分=IF(E32+E33<60,60,E32+E33) E32:基础评分（70%）, E33:行为习惯(30%)
	public static String score(double basicpoint ,double behavioralscore) {
		double sum = basicpoint + behavioralscore;
		DecimalFormat df = new DecimalFormat("0.0");
		return sum > 60 ?df.format(sum):"60";
	}
	
	//根据M31*M33/100/1000计算走了多长的距离其中M31表示步数,M33表示步距
	/**
	 * 
	 * @Title: totaldistance   
	 * @Description: 根据M31*M33/100/1000计算走了多长的距离其中M31表示步数,M33表示步距   
	 * @param sex 性别
	 * @param height 身高
	 * @param total 总步数
	 * @return      
	 * String      
	 * @throws
	 */
	public static String totaldistance(String sex,double height,int total) {
		double result = total*Double.parseDouble(stepdistance(sex,height))/100/1000;
		DecimalFormat df = new DecimalFormat("0.0");
		return df.format(new BigDecimal(result));
	}
	
	//速度（m/s）计算=M32*M33/100,其中M32表示不频率，M33表示步距
	
	/**
	 * 
	 * @Title: stepspeed   
	 * @Description: 速度（m/s）计算=M32*M33/100,其中M32表示不频率，M33表示步距 
	 * @param sex 性别
	 * @param height 身高
	 * @param ratio 步频率
	 * @return      
	 * String      
	 * @throws
	 */
	public static String stepspeed(String sex,double height,double ratio) {
		double result =  ratio * Double.parseDouble(stepdistance(sex,height)) / 100;
		DecimalFormat df = new DecimalFormat("0.0");
		return df.format(new BigDecimal(result));
	}
	
	//=O31/O32*1000/60所花步时长，分钟计算,其中o31表示距离，o32表示步速度
	/**
	 * 
	 * @Title: casttime   
	 * @Description: =O31/O32*1000/60所花步时长，分钟计算,其中o31表示距离，o32表示步速度   
	 * @param sex
	 * @param height
	 * @param ratio 步评率
	 * @return      
	 * String      
	 * @throws
	 */
	public static String casttime(String sex,double height,double ratio) {
	  double result =	Double.parseDouble(stepdistance(sex,height)) / Double.parseDouble(stepspeed(sex,height,ratio)) * 1000/60;
	  DecimalFormat df = new DecimalFormat("0.0");
	  return df.format(new BigDecimal(result));
	}
	
	//步距系数计算IF(B3="男",B4*0.415,B4*0.413)
		/**
		 * 
		 * @Title: stepdis   
		 * @Description: 步距系数计算IF(B3="男",B4*0.415,B4*0.413)，其中B4是身高
		 * @param stepnumber 步数
		 * @param distance 步距     
		 * void      
		 * @throws
		 */
		public static String stepdistance(String sex,double height) {
			double ratio = "男".equals(sex)?0.415:0.413;
			double result = height*ratio;
			DecimalFormat df = new DecimalFormat("0.0");
			return df.format(new BigDecimal(result));
		}
	
	/**
	 * 
	 * @Title: stepfrequencycoefficient   
	 * @Description: 步频系数=M32*M33/100 M32代表:速度,M33代表步距  
	 * @param rate
	 * @param distance
	 * @return      
	 * String      
	 * @throws
	 */
	public static String stepfrequencycoefficient(double rate,double distance) {
		double result = rate*distance/100 ;
		DecimalFormat df = new DecimalFormat("0");
		return df.format(new BigDecimal(result));
	}
	
	//步距  系数=O31/O32*1000/60
	/**
	 * 
	 * @Title: stepdistancecoefficient   
	 * @Description: 步距  系数=O31/O32*1000/60 O31代表步数系统,  O32代表:步频系数
	 * @param rate
	 * @param distance
	 * @return      
	 * String      
	 * @throws
	 */
	public static String stepdistancecoefficient(double stepnumbercoefficient,double stepfrequencycoefficient) {
		double result = stepnumbercoefficient/stepfrequencycoefficient*1000/60;
		DecimalFormat df = new DecimalFormat("0");
		return df.format(new BigDecimal(result));
	} 
	
	//卡路里=B5*O33*S45
	/**
	 * 
	 * @Title: calorie   
	 * @Description: 卡路里=B5*O33*S45   
	 * @param weigth 体重
	 * @param time_m 时间（分钟计算）
	 * @param indexk系数
	 * @return      
	 * String      
	 * @throws
	 */
	public static String calorie(double weigth,int time_m,double indexk) {
		double result = weigth*time_m*indexk;
		DecimalFormat df = new DecimalFormat("0.0");
		return df.format(new BigDecimal(result));
		
	}
	
//	//爬山所产生的卡里路计算
//	/**
//	 * 运动所产生的卡里路计算
//	 * @Title: walkforcalorie   
//	 * @Description: 运动所产生的卡里路计算B5*J33*(IF(ISERROR(VLOOKUP(V31,basic!R:S,2,0)),"",VLOOKUP(H32,basic!R:S,2,0)))其中B5表示体重，J32表示时长 分钟,其中的IF这部分是匹配系数 
//	 * @param code
//	 * @param weight
//	 * @param times
//	 * @return      
//	 * String      
//	 * @throws
//	 */
//	public static String mountainclimbingforcalorie(String code,double weight,int times) {
//		return sportforcalorie(code,weight,times);
//	}
	
	//运动产生的卡里路B5*J32*(IF(ISERROR(VLOOKUP(V31,basic!R:S,2,0)),"",VLOOKUP(H32,basic!R:S,2,0)))其中B5表示体重，J32表示时长 分钟
	/**
	 * 运动产生的卡里路
	 * @Title: walkforcalorie   
	 * @Description: 走路(散步)产生的卡里路B5*J32*(IF(ISERROR(VLOOKUP(V31,basic!R:S,2,0)),"",VLOOKUP(H32,basic!R:S,2,0)))其中B5表示体重，J32表示时长 分钟,其中的IF这部分是匹配系数 
	 * @param code 系数编码
	 * @param weight
	 * @param times
	 * @return      
	 * String      
	 * @throws
	 */
	public static String sportforcalorie(String code,double weight,int times) {
		double result = weight*times*Double.parseDouble(code);
		DecimalFormat df = new DecimalFormat("0");
		return df.format(new BigDecimal(result));
	}
	
	//运动评分:=IF(E10/(E11*0.2+(E11-E14)*0.2)*10>10,10,E10/(E11*0.2+(E11-E14)*0.2)*10)E10;运动消耗卡路里,E11:BMR（基础代谢率）E14:理想BMR
	/**        IF(IF(E10/(E11*0.2+(E11-E14)*0.2)*10>10,10,E10/(E11*0.2+(E11-E14)*0.2)*10)<0,0,IF(E10/(E11*0.2+(E11-E14)*0.2)*10>10,10,E10/(E11*0.2+(E11-E14)*0.2)*10))
	 * 
	 * @Title: sportscore   
	 * @Description: 运动打分   
	 * @param sportcalorie 运动消耗卡路里
	 * @param basicmetabolicrate BMR（基础代谢率）
	 * @param ideabmr 理想BMR
	 * @return      
	 * String      
	 * @throws
	 */
	public static String sportscore(double sportcalorie,double basicmetabolicrate ,double  ideabmr) {
		double result = sportcalorie /(basicmetabolicrate*0.2+(basicmetabolicrate-ideabmr)*0.2) * 10;
		DecimalFormat df = new DecimalFormat("0");
		String str_reslut =  df.format(new BigDecimal(result));
		return Double.parseDouble(str_reslut) > 10?"10":str_reslut;
	}
	
	//
	
	/**
	 * 
	 * @Title: foodcalorie   
	 * @Description:食物卡路里计算K16/100*(IF(ISERROR(VLOOKUP(J16,basic!R:S,2,0)),"",VLOOKUP(J16,basic!R:S,2,0)))其中K16食物的克数，J16是种类  
	 * @param foodtotal 食物克数
	 * @param code 食物系数
	 * @return      
	 * String      
	 * @throws
	 */
	public static String foodcalorie(double foodtotal,double code) {
		double result = foodtotal/100*code;
		DecimalFormat df = new DecimalFormat("0");
		return df.format(new BigDecimal(result));		
	}
	
	/**
	 * 
	 * @Title: foodratio   
	 * @Description:食物系统指标ROUND((M16-E11*1.1*0.3)/(E11*1.1*0.3),2) 其中M16是食物总克数 E11代表BMR
	 * @param foodtotal 食物总克数
	 * @param key 关键字早餐，中餐，午餐
	 * @return      
	 * String      
	 * @throws
	 */
	public static String foodratio(double foodtotal,double height, double weight, double age, String sex,String key) {
		double ratio = 0;
		if(DictionaryCode.LRTZ_YSRL_ZC.name().equals(key) ) {
			ratio = 0.3;
		}else if(DictionaryCode.LRTZ_YSRL_WC.name().equals(key) ) {
			ratio = 0.4;
		}else if(DictionaryCode.LRTZ_YSRL_WC1.name().equals(key) ) {
			ratio = 0.3;
		}else {
			ratio = 0;
		}
		if(ratio == 0) {
			DecimalFormat df = new DecimalFormat("0.00");
			return df.format(new BigDecimal(0));	
		}		
		double base_bmi = Double.parseDouble(basicmetabolicrate(height,weight, age, sex));
		double result = (double)(foodtotal- base_bmi *1.1*ratio)/(base_bmi*1.1*ratio);
		
		DecimalFormat df = new DecimalFormat("0.00");
		return df.format(new BigDecimal(result));		
	}
	
	//
	/**
	 * 
	 * @Title: foodSocre   
	 * @Description: 食物评分  IF(ABS(O16)<=0.1,10,IF(ABS(O16)<=0.2,5,IF(ABS(O16)<=0.3,0,IF(ABS(O16)<=0.4,-4,IF(ABS(O16)<=0.5,-5,IF(ABS(O16)<=0.6,-6,IF(ABS(O16)<=0.7,-7,IF(ABS(O16)<=0.8,-8,IF(ABS(O16)<-0.9,-9,IF(ABS(O16)<=1,-10))))))
	 * 其中O16表示食物系数 v2.0  =IF(ABS(O16)<=0.1,10,IF(ABS(O16)<=0.2,5,0))*0.3 
	 * @return      
	 * String      
	 * @throws
	 */
	public static String foodSocre(double foodratio) {
		double result = Math.abs(foodratio);
		double socre_result = 0;
		if(result <= 0.1) {
			socre_result = 10;
		}else if(result > 0.1 && result <= 0.2) {
			socre_result = 5;
		
		}else {
			socre_result = 0;
		}	
		DecimalFormat df = new DecimalFormat("0.00");
		return df.format(new BigDecimal(socre_result * 0.3));		
	}
	
	/**
	 * =IF(SUM(N16:N24)<-10,-10,SUM(N16:N24))N16-N24指早晚评分和
	 * @Title: foodSocreoftotal   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param foodratio_zc早餐平分
	 * @param foodratio_wc1 晚餐平分
	 * @param foodratio_wc 午餐评分
	 * @return      
	 * String      
	 * @throws
	 */
	public static String foodSocreoftotal(double foodratio_zc,double foodratio_wc1,double foodratio_wc) {
		double result = foodratio_zc + foodratio_wc1 + foodratio_wc;
		double socre_result = 0;
		if(result < -10) {
			socre_result = -10;
		}else {
			socre_result = result;
		}	
		DecimalFormat df = new DecimalFormat("0.00");
		return df.format(new BigDecimal(socre_result));		
	}
	
	//维持现有状况摄入卡路里=E11*E12+E10 其中E11表示BMR（基础代谢率），E12:BMR系数当前为常亮1.1,E10：运动消耗卡里路
	/**
	 * 
	 * @Title: maintaincurrentsituationcalorieintake   
	 * @Description: 维持现有状况摄入卡路里=E11*E12+E10 其中E11表示BMR（基础代谢率），E12:BMR系数当前为常亮1.1,E10：运动消耗卡里路
	 * @param basicBmr
	 * @param sportcalorie
	 * @return      
	 * String      
	 * @throws
	 */
	public static String maintaincurrentsituationcalorieintake(double basicBmr,double sportcalorie) {
		double result = basicBmr*1.1 + sportcalorie;
		DecimalFormat df = new DecimalFormat("0.00");
		return df.format(new BigDecimal(result));		
	}
	
	 //理想摄入卡路里=E14*E12+E10,E14:理想bmr，E12:BMR系数当前为常亮1.1，E10：运动消耗卡里路
	 
	 public static String ideacalorieintake(double ideaBmr,double sportcalorie) {
			double result = ideaBmr*1.1 + sportcalorie;
			DecimalFormat df = new DecimalFormat("0.00");
			return df.format(new BigDecimal(result));		
		}
	public static void main(String[] args) {

		System.out.println(IndexUtil.bmiIndex(165, 60) + ":bmi");
		System.out.println(IndexUtil.ageEffectOfIndex(28) + ":ageEffectOfIndex");
		
		System.out.println(IndexUtil.bodyfatrateIndex(165, 60, 28, "男") + ":bodyfatrateIndex");
		System.out.println(IndexUtil.healthscore(165, 60, 28) + ":healthscore");
		System.out.println(IndexUtil.pacelength("男", 165) + ":pacelength");
		System.out.println(IndexUtil.basicmetabolicrate(165, 60, 28, "男") + ":basicmetabolicrate");
		System.out.println(IndexUtil.idealweight(158) + ":idealweight");
		System.out.println(IndexUtil.idealBmr(165, 60, 28, "男") + ":idealBmr");
		System.out.println(IndexUtil.bloodpressureImpact(2) + ":bloodpressureImpact");
		System.out.println(
				IndexUtil.InfluenceOfbloodpressureforhighpressure(120) + ":InfluenceOfbloodpressureforhighpressure");
		System.out.println(
				IndexUtil.influenceOfbloodpressureforlowpressure(70) + ":InfluenceOfbloodpressureforlowpressure");
		System.out.println(IndexUtil.heartrate(162) + ":heartrate");
		System.out.println(IndexUtil.temperaturescore(39.8) + ":temperaturescore");
		System.out.println(IndexUtil.sleepduration(Lists.newArrayList(new PairVo<String, String>("2018/05/24 07:34", "2018/07/24 09:50")),
				"YYYY/MM/dd HH:mm") + ":sleepduration");
		System.out.println(IndexUtil.sleepTimescore(new PairVo<String, String>("2018/07/23 23:00", "2018/07/24 06:50"),
				"YYYY/MM/dd HH:mm") + ":sleepTimescore");
		System.out.println(IndexUtil.foodratio(580.0,178,67,31,"男",DictionaryCode.LRTZ_YSRL_ZC.name())+":foodratio");
	}
}
